package com.hspedu.newData.dmsxl.哈希表;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-27 
 * @Version: 1.0
 */

// 不能调用split方法
// todo
public class LC290单词规律 {

    public boolean wordPattern(String pattern, String s) {
        Map<Character,String> t2s = new HashMap<>();
        Map<String,Character> s2t = new HashMap<>();

        String[] split = s.split(" ");

        int len1 = pattern.length();
        int len2 = split.length;
        if (len1 != len2) return false;

        for (int i = 0; i < len1; i++) {
            char ch = pattern.charAt(i);
            String str = split[i];

            if ((s2t.containsKey(str) && s2t.get(str) != ch) || (t2s.containsKey(ch) && !t2s.get(ch).equals(str))){
                return false;
            }
            s2t.put(str,ch);
            t2s.put(ch,str);
        }
        return true;
    }


    @Test
    public void test() {
        // 测试代码
        boolean b = wordPattern("abba", "dog cat cat dog");
        System.out.println("");
    }
}
